//
//  KDStartAndEndView.m
//  LaiKeDaoClient
//
//  Created by dongkedi的iMac on 15/11/6.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "KDStartAndEndView.h"
#import "NSDate+Helper.h"

@implementation KDStartAndEndView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createUI];
        [self createThreeButton];
        
        self.chooseDateView = [[KDChooseDateView alloc] init];
        self.chooseDateView.sureDelegate = self;
    }
    return self;
}

- (void)createUI
{
    UILabel *label1 = [[UILabel alloc] initWithFrame:(CGRect){45/2,35/2,100,12}];
    [label1 setTextAlignment:NSTextAlignmentLeft];
    [label1 setFont:[UIFont systemFontOfSize:12]];
    [label1 setBackgroundColor:[UIColor clearColor]];
    [label1 setTextColor:[UIColor colorWithHexString:@"#aaaaaa"]];
    [label1 setText:@"开始"];
    [self addSubview:label1];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:(CGRect){kScreenWidth/2+10,Y(label1),100,12}];
    [label2 setTextAlignment:NSTextAlignmentLeft];
    [label2 setFont:[UIFont systemFontOfSize:12]];
    [label2 setBackgroundColor:[UIColor clearColor]];
    [label2 setTextColor:[UIColor colorWithHexString:@"#aaaaaa"]];
    [label2 setText:@"截止"];
    [self addSubview:label2];
    
    self.startLabel = [[UILabel alloc] initWithFrame:(CGRect){X(label1),Y(label1)+HEIGHT(label1)+15,90,17}];
    [_startLabel setTextAlignment:NSTextAlignmentLeft];
    [_startLabel setFont:[UIFont systemFontOfSize:17]];
    [_startLabel setBackgroundColor:[UIColor clearColor]];
    [_startLabel setTextColor:[UIColor colorWithHexString:@"#222222"]];
    [_startLabel setText:@"开始日期"];
    [self addSubview:_startLabel];
    
    self.endLabel = [[UILabel alloc] initWithFrame:(CGRect){X(label2),Y(label2)+HEIGHT(label2)+15,90,17}];
    [_endLabel setTextAlignment:NSTextAlignmentLeft];
    [_endLabel setFont:[UIFont systemFontOfSize:17]];
    [_endLabel setBackgroundColor:[UIColor clearColor]];
    [_endLabel setTextColor:[UIColor colorWithHexString:@"#222222"]];
    [_endLabel setText:@"截止日期"];
    [self addSubview:_endLabel];
    
    self.startWeekLabel = [[UILabel alloc] initWithFrame:(CGRect){X(_startLabel)+WIDTH(_startLabel)+3,Y(_startLabel),25,12}];
    [_startWeekLabel setTextAlignment:NSTextAlignmentLeft];
    [_startWeekLabel setFont:[UIFont systemFontOfSize:12]];
    [_startWeekLabel setBackgroundColor:[UIColor clearColor]];
    [_startWeekLabel setTextColor:[UIColor colorWithHexString:@"#aaaaaa"]];
    [_startWeekLabel setText:@"开始"];
    [self addSubview:_startWeekLabel];
    
    self.endWeekLabel = [[UILabel alloc] initWithFrame:(CGRect){X(_endLabel)+WIDTH(_endLabel)+3,Y(_endLabel),25,12}];
    [_endWeekLabel setTextAlignment:NSTextAlignmentLeft];
    [_endWeekLabel setFont:[UIFont systemFontOfSize:12]];
    [_endWeekLabel setBackgroundColor:[UIColor clearColor]];
    [_endWeekLabel setTextColor:[UIColor colorWithHexString:@"#aaaaaa"]];
    [_endWeekLabel setText:@"结束"];
    [self addSubview:_endWeekLabel];
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:(CGRect){X(_startWeekLabel)+WIDTH(_startWeekLabel)+6,Y(_startWeekLabel),11/2,20/2}];
    [image1 setImage:[UIImage imageWithName:@"btn_time-set.png"]];
    [self addSubview:image1];
    
    UIImageView *image2 = [[UIImageView alloc] initWithFrame:(CGRect){X(_endWeekLabel)+WIDTH(_endWeekLabel)+6,Y(_endWeekLabel),11/2,20/2}];
    [image2 setImage:[UIImage imageWithName:@"btn_time-set.png"]];
    [self addSubview:image2];
}

- (void)createThreeButton
{
    float   ButtonWidth = 40;
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setFrame:(CGRect){X(_startWeekLabel),Y(_startWeekLabel),ButtonWidth,25}];
    [button1 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [button1 setBackgroundColor:[UIColor clearColor]];
//    [button1.titleLabel setTextColor:[UIColor whiteColor]];
//    [button1 setTitle:@"确  定" forState:UIControlStateNormal];
    [self addSubview:button1];
    button1.tag = 101;
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setFrame:(CGRect){X(_endWeekLabel),Y(_endWeekLabel),ButtonWidth,25}];
    [button2 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [button2 setBackgroundColor:[UIColor clearColor]];
    [self addSubview:button2];
    button2.tag = 102;
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button3 setFrame:(CGRect){X(_startLabel),Y(_startLabel)+HEIGHT(_startLabel)+15,550/2,70/2}];
    [button3 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [button3 setBackgroundColor:kNavBlueColor];
    [button3.titleLabel setTextColor:[UIColor whiteColor]];
    [button3 setTitle:@"确  定" forState:UIControlStateNormal];
    [self addSubview:button3];
    button3.tag = 103;
}

- (void)buttonAction1:(UIButton *)button
{
    switch (button.tag) {
        case 101:{
            self.chooseDateView.titleLabel.text = @"请选择开始日期";
            [self.chooseDateView show];
        }
            break;
        case 102:{
            self.chooseDateView.titleLabel.text = @"请选择截止日期";
            [self.chooseDateView show];
        }
            break;
        case 103:{
            if ([_startLabel.text isEqualToString:@"开始日期"] || [_endLabel.text isEqualToString:@"截止日期"]) {
                [self showAlert:@"请选择开始和截止日期"];
            }else{
                if ([self.startAndEndDelegate respondsToSelector:@selector(startAndEndViewSureButton:endDate:)]) {
                    [self.startAndEndDelegate startAndEndViewSureButton:_startLabel.text endDate:_endLabel.text];
                }
            }
        }
            
            break;
        
        default:
            break;
    }
}

- (void)showAlert:(NSString *)str
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}

- (void)clickSure:(NSString *)dateStr
{
    if ([self.chooseDateView.titleLabel.text isEqualToString:@"请选择开始日期"]) {
        self.startLabel.text = dateStr;
        self.startWeekLabel.text = [self getWeekDay:dateStr];
    }else{
        self.endLabel.text = dateStr;
        self.endWeekLabel.text = [self getWeekDay:dateStr];
    }
}

- (NSString *)getWeekDay:(NSString *)dateStr
{
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    NSArray *dateArr = [dateStr componentsSeparatedByString:@"-"];
    NSDateComponents *_comps = [[NSDateComponents alloc] init];
    [_comps setDay:[dateArr.lastObject integerValue]];
    [_comps setMonth:[[dateArr objectAtIndex:1] integerValue]];
    [_comps setYear:[dateArr.firstObject integerValue]];
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *_date = [gregorian dateFromComponents:_comps];
    NSDateComponents *weekdayComponents =
    [gregorian components:NSWeekdayCalendarUnit fromDate:_date];
    int _weekday = [weekdayComponents weekday];
    
    return [weekdays objectAtIndex:_weekday];
}
@end
